package com.cjkj.equipment.utils;

import com.alibaba.fastjson2.JSONObject;
import com.cjkj.common.core.utils.StringUtils;
import com.cjkj.equipment.domain.MonitorPadData;
import com.cjkj.equipment.domain.SmartBraceletData;
import com.cjkj.equipment.domain.dto.EquipmentStatusDto;
import com.cjkj.equipment.domain.dto.MonitorPadDto;
import com.cjkj.equipment.domain.dto.SmartBraceletDto;
import com.cjkj.system.api.domain.dto.APIEquipmentStatusDto;
import org.springframework.beans.BeanUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通过日志恢复监测数据
 * @author cjkj
 * @date 2024-08-30
 */
public class GetLogUtil {
    public JSONObject stream() {
        // 日志文件地址
        String fileName = "C:\\Users\\93000\\Desktop\\fsdownload\\warn.log";

        // 智能手环数据
        List<String> smartBraceletContent = new ArrayList<>();
        // 智能床垫数据
        List<String> monitorPadContent = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(fileName)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    String[] split = line.split(" - ");
                    if (split.length == 3) {
                        String s = split[0];
                        if(s.contains("MonitorPadDataServiceImpl")) {
                            monitorPadContent.add(split[2]);
                        }

                        if(s.contains("SmartBraceletDataServiceImpl")) {
                            smartBraceletContent.add(split[2]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("获取到的智能手环消息内容条数：" + smartBraceletContent.size());
        System.out.println("获取到的睡眠监测垫消息内容条数：" + monitorPadContent.size());

        List<MonitorPadData> monitorPadData = addMonitorPadData(monitorPadContent);
        List<SmartBraceletData> smartBraceletData = addSmartBraceletData(smartBraceletContent);

        JSONObject info = new JSONObject();
        info.put("monitorPadData", monitorPadData);
        info.put("smartBraceletData", smartBraceletData);
        return info;
    }

    private List<MonitorPadData> addMonitorPadData(List<String> monitorPadContent){
        // 睡眠垫数据
        List<MonitorPadData> monitorPadDataList = new ArrayList<>();
        int monitorPadInvalidData = 0;
        for (String content : monitorPadContent) {
            MonitorPadDto monitorPadDto = JSONObject.parseObject(content, MonitorPadDto.class);
            MonitorPadData data = getDataFromDto(monitorPadDto);
            if(data != null) {
                monitorPadDataList.add(data);
            } else {
                monitorPadInvalidData += 1;
            }
        }

        System.out.println("睡眠垫有效数据条数：" + monitorPadDataList.size() +"，睡眠垫无效数据条数：" + monitorPadInvalidData);
        return monitorPadDataList;
    }

    private List<SmartBraceletData> addSmartBraceletData(List<String> smartBraceletContent){
        // 智能手环数据
        List<SmartBraceletData> smartBraceletDataList = new ArrayList<>();
        int smartBraceletInvalidData = 0;
        for (String content: smartBraceletContent) {
            SmartBraceletDto smartBraceletDto = JSONObject.parseObject(content,SmartBraceletDto.class);
            EquipmentStatusDto equipmentStatusDto = JSONObject.parseObject(content,EquipmentStatusDto.class);
            APIEquipmentStatusDto apiEquipmentStatusDto = new APIEquipmentStatusDto();
            BeanUtils.copyProperties(equipmentStatusDto, apiEquipmentStatusDto);
            SmartBraceletData data = getDataFromDto(smartBraceletDto);
            if(data != null) {
                smartBraceletDataList.add(data);
            } else {
                smartBraceletInvalidData += 1;
            }
        }
        System.out.println("智能手环有效数据条数：" + smartBraceletDataList.size() +"，智能手环无效数据条数：" + smartBraceletInvalidData);

        return smartBraceletDataList;
    }

    private MonitorPadData getDataFromDto(MonitorPadDto dto){
        if (dto.getItems() == null) {
            return null;
        }
        MonitorPadData data = new MonitorPadData();
        data.setEquipmentNo(dto.getEquipmentNo());
        data.setProductKey(dto.getProductKey());
        data.setCreateTime(new Date());

        if(dto.getItems().getBreathRate() != null){
            data.setBreathRate(Integer.parseInt(dto.getItems().getBreathRate().getValue()));
            data.setAcquisitionTime(dto.getItems().getBreathRate().getTime());
        }
        if(dto.getItems().getHeartRate() != null){
            data.setHeartRate(Integer.parseInt(dto.getItems().getHeartRate().getValue()));
            data.setAcquisitionTime(dto.getItems().getHeartRate().getTime());
        }
        if(dto.getItems().getSpo2() != null){
            data.setSpo2(Integer.parseInt(dto.getItems().getSpo2().getValue()));
            data.setAcquisitionTime(dto.getItems().getSpo2().getTime());
        }
        if(dto.getItems().getBodyMove() != null){
            data.setBodyMove(dto.getItems().getBodyMove().getValue());
            data.setAcquisitionTime(dto.getItems().getBodyMove().getTime());
        }
        if(dto.getItems().getSleepStatus() != null){
            data.setSleepStatus(dto.getItems().getSleepStatus().getValue());
            data.setAcquisitionTime(dto.getItems().getSleepStatus().getTime());
        }
        if(dto.getItems().getEquipmentFailure() != null){
            data.setEquipmentFailure(dto.getItems().getEquipmentFailure().getValue());
            data.setAcquisitionTime(dto.getItems().getEquipmentFailure().getTime());
        }
        return data;
    }

    private SmartBraceletData getDataFromDto(SmartBraceletDto dto){
        if (dto.getItems() == null) {
            return null;
        }
        SmartBraceletData data = new SmartBraceletData();
        data.setEquipmentNo(dto.getEquipmentNo());
        data.setProductKey(dto.getProductKey());
        data.setCreateTime(new Date());
        if(dto.getItems().getWearState() != null){
            data.setWearState(dto.getItems().getWearState().getValue());
            data.setAcquisitionTime(dto.getItems().getWearState().getTime());
        }
        if(dto.getItems().getChargeStatus() != null){
            data.setChargeStatus(dto.getItems().getChargeStatus().getValue());
            data.setAcquisitionTime(dto.getItems().getChargeStatus().getTime());
        }
        if(dto.getItems().getBatteryPercentage() != null){
            data.setBatteryPercentage(Integer.parseInt(dto.getItems().getBatteryPercentage().getValue()));
            data.setAcquisitionTime(dto.getItems().getBatteryPercentage().getTime());
        }
        if(dto.getItems().getLowPowerFlag() != null){
            data.setLowPowerFlag(dto.getItems().getLowPowerFlag().getValue());
            data.setAcquisitionTime(dto.getItems().getLowPowerFlag().getTime());
        }
        if(dto.getItems().getOperatingMode() != null){
            data.setOperatingMode(dto.getItems().getOperatingMode().getValue());
            data.setAcquisitionTime(dto.getItems().getOperatingMode().getTime());
        }
        if(dto.getItems().getSpo2() != null){
            data.setSpo2(Integer.parseInt(dto.getItems().getSpo2().getValue()));
            data.setAcquisitionTime(dto.getItems().getSpo2().getTime());
        }
        if(dto.getItems().getFallState() != null){
            data.setFallState(dto.getItems().getFallState().getValue());
            data.setAcquisitionTime(dto.getItems().getFallState().getTime());
        }
        if(dto.getItems().getHeartRate() != null){
            data.setHeartRate(Integer.parseInt(dto.getItems().getHeartRate().getValue()));
            data.setAcquisitionTime(dto.getItems().getHeartRate().getTime());
        }
        if(dto.getItems().getTemperature() != null){
            data.setTemperature(Integer.parseInt(dto.getItems().getTemperature().getValue()));
            data.setAcquisitionTime(dto.getItems().getTemperature().getTime());
        }
        return data;
    }

}
